import sys
import csv

#input #CGS    SEQ     SAMPLE  CHR     SPOS    EPOS    GENE    TYPE    NOTES
#['YCGS1', 'YSEQ4', 'YO502', 'mito', '69188', '69188', 'tA(UGC)Q', 'INTERGENIC', 'trf:84;dust:91;trf:73', ' tA(UGC)Q', 'PROMOTER -657', '', '']

#mutation format
#chr	start	end	sample	type	gene	notes	Sequencing plate
#chrI	603	603	YO_502:YCGS1:YSEQ4	Missense intergenic	C:T	YCGS1:YSEQ4:YO502
#chrI	706	706	YO_502:YCGS1:YSEQ4	Nonsynonymous	YAL067W-A	TAG:  TgG:W U	YCGS1:YSEQ4:YO502
#chrI	1000	1000	SilentMissense	Silent		Silent	A:T	SomePlate
#chrI	1200	1300	Deletion	Deletion	Notes	.	SomePlate
#chrI	1305	1305	Insertion	Insertion		Notes	A	YCGS1:YSEQ4
#chrII	20	30	Nonsense	Nonsense	NSInsert	AAAAAAAAAA	YCGS2:YSEQ5
#chrII	35	35	YO_502_Unknown	Unknown		UnknownDeletion	Missense	G	Plate
#chrII	40	44	YO_502	Indel	SST1	IndelNotes	A	Plate
#chrII	50	51	YO_502_Missense	Missense	TLC1	Notes	C	Well01
#chrII	60	65	YO_502	Synonymous	YBR284W	Notes	T	Well02
#chrII	70	71	YO_502_Unknown	Target		NotesNoGene	G	Plate2
#chrII	75	77	YO_502_Targeted	Targeted_Region	Notes	G	Plate2
#chrII	80	82	YO_502_Splice	Splice_site	Notes	A	Plate3
chrhash = {}
chrhash["2u"] = "2micron"
chrhash["mito"] = "chrM"
chrhash["1"] = "chrI"
chrhash["2"] = "chrII"
chrhash["3"] = "chrIII"
chrhash["4"] = "chrIV"
chrhash["5"] = "chrV"
chrhash["6"] = "chrVI"
chrhash["7"] = "chrVII"
chrhash["8"] = "chrVIII"
chrhash["9"] = "chrIX"
chrhash["10"] = "chrX"
chrhash["11"] = "chrXI"
chrhash["12"] = "chrXII"
chrhash["13"] = "chrXIII"
chrhash["14"] = "chrXIV"
chrhash["15"] = "chrXV"
chrhash["16"] = "chrXVI"

def translate2mut(infile, outfile, label):
	tsv_file = csv.reader(open(infile, 'rb'),delimiter='\t')
	mutwriter = open(outfile, 'w')
	mutwriter.write('chr\tstart\tend\tsample\ttype\tgene\tpromoter\tclassify_notes\n')
	for line in tsv_file:
		chr = chrhash[line[3]]		
		myout = chr + '\t' + line[4]+ '\t' + line[5] + '\t' + label + '_'+line[2]+':'+line[0]+'_'+line[7] + '\t' + line[7] + '\t' + line[6] +'\t' + line[10] + '\t' + line[8] + ' ' + line[9]  + ' ' + line[11] + ' ' + line[12] 
		#line.split('\t'); #fprint '\t '.join(line)
		print myout
		mutwriter.write(myout+'\n')		
	mutwriter.close()

if __name__ == '__main__':
	infile = sys.argv[1]
	outfile = sys.argv[2]
	sourced_label = sys.argv[3]
	translate2mut(infile, outfile, sourced_label)	       

